//
//  CZWelcomeViewController.swift
//  CZWeibo
//
//  Created by 周振兰 on 16/5/14.
//  Copyright © 2016年 周振兰. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class CZWelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

    perpareUI()
        setIcon()
          //获取用户信息
        CZUserAccountViewModel.shareIntance.loadUserInfo { (error) -> () in
            self.setIcon()
        }
        
    }
  
    func setIcon ()
    {
        if let avatar_large = CZUserAccountViewModel.shareIntance.userAccount?.avatar_large{
       print("\(avatar_large)")
            //获取数据，设置头像
            let url = NSURL(string: avatar_large)
            self.iconView.sd_setImageWithURL(url,placeholderImage:UIImage(named: "avatar_default_big"))
        }
    }
    
    override func viewDidAppear(animated: Bool) {
        //在用户看到界面的时候在调用
        moveAnimation()
    }
    // 移动动画
    func moveAnimation()
    {
        //弹簧动画
      UIView.animateWithDuration(2, delay: 2, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
        //要用动画
        //更新约束
        self.iconView.snp_updateConstraints(closure: { (make) -> Void in
            make.bottom.equalTo(self.view).offset(-(UIScreen.mainScreen().bounds.height-160))
        })
        
        self.view.layoutIfNeeded()
        
        }) { (_) -> Void in
            CZPrint(items: "动画完成")
            //跳转到CZWelcomeViewCotroller
            let appDelegate =  (UIApplication.sharedApplication().delegate as! AppDelegate)
            
            appDelegate.switchRootViewController(CZMainTableBalController())
        }
    }
    
    
    func perpareUI()
    {
        view.addSubview(bkgView)
        view.addSubview(messageLabel)
        view.addSubview(iconView)
         //背景约束
        bkgView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self.view)
            //头像
            iconView.snp_makeConstraints(closure: { (make) -> Void in
                make.centerX.equalTo(self.view)
                make.bottom.equalTo(self.view).offset(-160)
                
                //消息
                messageLabel.snp_makeConstraints(closure: { (make) -> Void in
                    make.centerX.equalTo(iconView)
                    make.top.equalTo(iconView.snp_bottom).offset(16)
                })
            })
        }
    }

    //MARK :- 懒加载
    //背景
    private  lazy var bkgView:UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    //头像
    lazy var iconView : UIImageView =
        {
            let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
            //圆角
            imageView.layer.cornerRadius = 42.5
            imageView.layer.masksToBounds = true
            return imageView
            
    }()
    //消息lable
    lazy var messageLabel:UILabel =
    {
        let lable1 = UILabel()
        //设置lable
        lable1.text = "欢迎归来"
        lable1.textColor = UIColor.blackColor()
        lable1.font =    UIFont.systemFontOfSize(15)
        lable1.sizeToFit()
     
        return lable1
       
    }()
    
}

